//
//  MJTabBar.swift
//  weibo
//
//  Created by mojue on 16/6/23.
//  Copyright © 2016年 NetLife. All rights reserved.
//

import UIKit

class MJTabBar: UITabBar {

   // 在创建的时候添加子控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 定义一个索引 用来记录子控件的x
        var index: CGFloat = 0
        
        // 设置item的宽高
        let width = bounds.width / 5
        let height = bounds.height
        
        // 遍历子控件
        for subview in subviews {
            
            print(subview)
            // UITabBarButton -- 是系统的私有类,不对外公开
            // isKindOfClass 用于判断 是不是 这个类
            // UITabBarButton 怎么和 AnyClass 对应起来
            // NSClassFromString("UITabBarButton")! 为什么要强制解包?
            
            
            if subview .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRectMake(index * width, 0, width, height)
                
                index += 1
                
                if index == 2 { index += 1 }
            }
            
        }
        
        composeBtn.frame = CGRectMake(2 * width, 0, width, height)
        
    }
    
    lazy var composeBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        
        // 设置属性
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState:UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal) 
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        // 添加点击事件
        btn.addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 返回控件
        return btn
        
        
        
    }()
    
    func click(btn: UIButton) {
        
    }
    
    
}
